package com.herench.bunch.lab.convert;


import ca.uhn.hl7v2.model.Message;
import ca.uhn.hl7v2.model.v24.datatype.CE;
import ca.uhn.hl7v2.model.v24.group.ORM_O01_ORDER_DETAIL;
import ca.uhn.hl7v2.model.v24.message.ORM_O01;
import ca.uhn.hl7v2.model.v24.segment.*;
import com.herench.bunch.commons.support.RequestMessage;
import com.herench.bunch.commons.utils.IdGenerator;

import java.util.Date;


public class LabSendConvert extends AbstractMessageConvert {

    @Override
    public Object toMessage(RequestMessage requestMessage) throws Exception {
        return null;
    }

    @Override
    public Message generatorReqMessage() throws Exception {

        ORM_O01 qry = new ORM_O01();
        qry.initQuickstart("ORM_O01", "O01", "A");
        MSH msh = qry.getMSH();
        msh.getMsh15_AcceptAcknowledgmentType().setValue("1");//actionType
        msh.getMsh11_ProcessingID().getPt1_ProcessingID().setValue("400010"); //msgCode
        msh.getMsh11_ProcessingID().getPt2_ProcessingMode().setValue(IdGenerator.getUUID());
        msh.getMsh3_SendingApplication().getHd1_NamespaceID().setValue("HIS");//sender
        msh.getMsh7_DateTimeOfMessage().getTs1_TimeOfAnEvent().setValue(new Date());
        msh.getMsh10_MessageControlID().setValue(IdGenerator.getUUID());

        PID pid = qry.getPATIENT().getPID();
        pid.getPatientID().getCx1_ID().setValue("10008513");//patientId
        pid.getPatientID().getCx2_CheckDigit().setValue("");//visitId

        pid.getPatientID().getCx3_CodeIdentifyingTheCheckDigitSchemeEmployed().setValue("");//bedLabel

        pid.getPatientID().getCx5_IdentifierTypeCode().setValue("");//babyNo 婴儿号


        pid.getPid5_PatientName(0).getXpn1_FamilyName().getFn3_OwnSurname().setValue("马会斌");//name
        pid.getPid8_AdministrativeSex().setValue("男");//sex

        pid.getPid7_DateTimeOfBirth().getTs1_TimeOfAnEvent().setValue("1955-03-14"); //dateOfBirth
        pid.getDateTimeOfBirth().getTs2_DegreeOfPrecision().setValue("63岁"); //age 带单位

        pid.getPid11_PatientAddress(0).getXad1_StreetAddress().getSad1_StreetOrMailingAddress().setValue(""); //mailingAddress


        ORC orc = qry.getORDER().getORC();
        orc.getOrc1_OrderControl().setValue("M12311000000AG"); //testNo
        orc.getOrc5_OrderStatus().setValue("2");//resultStatus
        orc.getOrc7_QuantityTiming(0).getTq4_StartDateTime().getTs1_TimeOfAnEvent().setValue("20180918085343");//requestedDateTime

        orc.getOrc7_QuantityTiming(0).getTq8_Text().setValue("检验申请");//医嘱类别

        orc.getOrc12_OrderingProvider(0).getXcn2_FamilyName().getFn1_Surname().setValue("李春峰"); //orderingProvider

        orc.getOrc17_EnteringOrganization().getCe1_Identifier().setValue("01010201");//orderingDept;

        orc.getOrc15_OrderEffectiveDateTime().getTs1_TimeOfAnEvent().setValue("");// spcmSampleDateTime

        orc.getOrc16_OrderControlCodeReason().getText().setValue("血清");//specimen

        orc.getOrc16_OrderControlCodeReason().getCe4_AlternateIdentifier().setValue("0101030302");//performBy

        orc.getOrc20_AdvancedBeneficiaryNoticeCode().getCe2_Text().setValue("1");//billingIndicator

        orc.getOrc2_PlacerOrderNumber().getEi3_UniversalID().setValue("0"); //priorityIndicator


        ORM_O01_ORDER_DETAIL detail = qry.getORDER().getORDER_DETAIL();

        NTE nte = detail.getNTE();
        nte.getNte3_Comment(0).setValue("");//notesForSpcm
        nte.getNte3_Comment(1).setValue("");//bottleColor

        DG1 dg1 = detail.getDG1();
        dg1.getDg14_DiagnosisDescription().setValue("脑梗塞");//relevantClinicDiag

        ODT odt = detail.getODT();


        CE ce1 = odt.insertServicePeriod(0);
        ce1.getCe1_Identifier().setValue("item");
        ce1.getCe2_Text().setValue("itemNo=1&itemName=肝功能(12项)&itemCode=6883201806220001&sheetTitle=&sheetId=&costs=72.0000&charges=72.0000");
        CE ce2 = odt.insertServicePeriod(1);
        ce2.getCe1_Identifier().setValue("item");

        ce2.getCe2_Text().setValue("itemNo=1&itemName=肾功能&itemCode=6883201806220002&sheetTitle=&sheetId=&costs=28.0000&charges=28.0000");
        CE ce3 = odt.insertServicePeriod(2);
        ce3.getCe1_Identifier().setValue("item");

        ce3.getCe2_Text().setValue("itemNo=1&itemName=血脂&itemCode=6883201806220003&sheetTitle=&sheetId=&costs=34.0000&charges=34.0000");
        CE ce4 = odt.insertServicePeriod(3);
        ce4.getCe1_Identifier().setValue("item");
        ce4.getCe2_Text().setValue("itemNo=1&itemName=葡萄糖测定&itemCode=688320180622000T&sheetTitle=&sheetId=&costs=15.0000&charges=15.0000");

        return qry;
    }

    @Override
    public Message fromMessage(Object message) throws Exception {
        return null;
    }


}
